package net.technics;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.hibernate.HibernateUtil;
import net.models.Application;
import net.models.Groups;
import net.models.Users;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.hibernate.Transaction;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.BeanProperties;

public class UtilisateurDelete {
	private DataBindingContext m_bindingContext;

	protected Shell	shlBackOfficeManagement;
	private Text txtSupprimerUnUtilisateur;
	private Text textnom;
	private Text textprenom;
	private Text textadresse;
	private Text textcp;
	private Text textville;
	private Text textusername;
	private Text textpassword;
	private List<Users> users = new ArrayList<Users>();
	private List<Groups> groups = new ArrayList<Groups>();
	private Users user = new Users();
	private ComboViewer comboViewerUtils;
	private ComboViewer comboViewergroup;
	private DateTime dateTime;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					UtilisateurDelete window = new UtilisateurDelete();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		users = Application.getUsers();
		groups = Application.getGroups();
		Display display = Display.getDefault();
		createContents();
		shlBackOfficeManagement.open();
		shlBackOfficeManagement.layout();
		while (!shlBackOfficeManagement.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlBackOfficeManagement = new Shell();
		shlBackOfficeManagement.setText("Back Office Management");
		shlBackOfficeManagement.setSize(359, 510);
		
		Composite composite = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		composite.setBounds(0, 0, 342, 48);
		
		txtSupprimerUnUtilisateur = new Text(composite, SWT.READ_ONLY);
		txtSupprimerUnUtilisateur.setText("Supprimer un utilisateur");
		txtSupprimerUnUtilisateur.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		txtSupprimerUnUtilisateur.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.BOLD));
		txtSupprimerUnUtilisateur.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_BLUE));
		txtSupprimerUnUtilisateur.setBounds(10, 10, 216, 21);
		
		Composite composite_1 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_1.setBounds(10, 54, 332, 37);
		
		CLabel lblUtilisateurSupprimer = new CLabel(composite_1, SWT.NONE);
		lblUtilisateurSupprimer.setText("Utilisateur à supprimer");
		lblUtilisateurSupprimer.setBounds(0, 8, 156, 21);
		
		comboViewerUtils = new ComboViewer(composite_1, SWT.NONE);
		Combo comboutil = comboViewerUtils.getCombo();
		comboutil.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection sel = (IStructuredSelection) (comboViewerUtils.getSelection());
				user = (Users) (sel.iterator().next());
				Users util = user;

				textnom.setEnabled(true);
				textnom.setText(user.getName());
				textprenom.setEnabled(true);
				textprenom.setText(user.getPrenom());
				textadresse.setEnabled(true);
				textadresse.setText(user.getAdresse());
				textcp.setEnabled(true);
				textcp.setText(user.getCp());
				textville.setEnabled(true);
				textville.setText(user.getVille());
				textusername.setEnabled(true);
				textusername.setText(user.getUsername());
				textpassword.setEnabled(true);
				textpassword.setText(user.getPassword());
				comboViewergroup.setSelection(new StructuredSelection(user.getGroups()));
				m_bindingContext = initDataBindingsDate();
			}
		});
		comboutil.setBounds(160, 5, 162, 28);
		
		Composite composite_2 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_2.setBounds(10, 96, 332, 314);
		
		CLabel label_1 = new CLabel(composite_2, SWT.NONE);
		label_1.setText("Nom");
		label_1.setBounds(10, 10, 61, 21);
		
		CLabel label_2 = new CLabel(composite_2, SWT.NONE);
		label_2.setText("Prénom");
		label_2.setBounds(10, 43, 61, 21);
		
		CLabel label_3 = new CLabel(composite_2, SWT.NONE);
		label_3.setText("Adresse");
		label_3.setBounds(10, 76, 61, 21);
		
		CLabel label_4 = new CLabel(composite_2, SWT.NONE);
		label_4.setText("Code postal");
		label_4.setBounds(10, 109, 99, 21);
		
		CLabel label_5 = new CLabel(composite_2, SWT.NONE);
		label_5.setText("Ville");
		label_5.setBounds(10, 142, 61, 21);
		
		CLabel label_6 = new CLabel(composite_2, SWT.NONE);
		label_6.setText("Date d'embauche");
		label_6.setBounds(10, 175, 124, 21);
		
		CLabel label_7 = new CLabel(composite_2, SWT.NONE);
		label_7.setText("Username");
		label_7.setBounds(10, 208, 99, 21);
		
		CLabel label_8 = new CLabel(composite_2, SWT.NONE);
		label_8.setText("Password");
		label_8.setBounds(10, 241, 68, 21);
		
		CLabel label_9 = new CLabel(composite_2, SWT.NONE);
		label_9.setText("Groupe");
		label_9.setBounds(10, 274, 61, 21);
		
		textnom = new Text(composite_2, SWT.BORDER);
		textnom.setEnabled(false);
		textnom.setBounds(158, 10, 164, 26);
		
		textprenom = new Text(composite_2, SWT.BORDER);
		textprenom.setEnabled(false);
		textprenom.setBounds(158, 43, 164, 26);
		
		textadresse = new Text(composite_2, SWT.BORDER);
		textadresse.setEnabled(false);
		textadresse.setBounds(158, 76, 164, 26);
		
		textcp = new Text(composite_2, SWT.BORDER);
		textcp.setEnabled(false);
		textcp.setBounds(158, 109, 164, 26);
		
		textville = new Text(composite_2, SWT.BORDER);
		textville.setEnabled(false);
		textville.setBounds(158, 142, 164, 26);
		
		textusername = new Text(composite_2, SWT.BORDER);
		textusername.setEnabled(false);
		textusername.setBounds(158, 208, 164, 26);
		
		textpassword = new Text(composite_2, SWT.BORDER);
		textpassword.setEnabled(false);
		textpassword.setBounds(158, 241, 164, 26);
		
		comboViewergroup = new ComboViewer(composite_2, SWT.NONE);
		Combo combogroup = comboViewergroup.getCombo();
		combogroup.setBounds(158, 274, 164, 28);
		
		dateTime = new DateTime(composite_2, SWT.BORDER);
		dateTime.setBounds(158, 175, 164, 28);
		
		Composite composite_3 = new Composite(shlBackOfficeManagement, SWT.NONE);
		composite_3.setBounds(124, 414, 218, 52);
		
		Button btnSupprimer = new Button(composite_3, SWT.NONE);
		btnSupprimer.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				IStructuredSelection sel = (IStructuredSelection) (comboViewerUtils.getSelection());
				user = (Users) (sel.iterator().next());
				Users util = user;

				org.hibernate.Session session = (org.hibernate.Session) HibernateUtil.getSession();
				Transaction trans = session.beginTransaction();
				IStructuredSelection sel1 = (IStructuredSelection) (comboViewergroup.getSelection());
				Groups group = (Groups) (sel1.iterator().next());
				Calendar instance = Calendar.getInstance();
				instance.set(Calendar.DAY_OF_MONTH, dateTime.getDay());
				instance.set(Calendar.MONTH, dateTime.getMonth());
				instance.set(Calendar.YEAR, dateTime.getYear());
				Users user = new Users(util.getId(), textnom.getText(), textprenom.getText(), 
						textadresse.getText(), textcp.getText(), textville.getText(),instance.getTime(), textusername.getText(), 
						textpassword.getText(),group);
				session.delete(user);
				trans.commit();
				session.close();
				shlBackOfficeManagement.close();
			}
		});
		btnSupprimer.setText("Valider");
		btnSupprimer.setBounds(22, 10, 90, 30);
		
		Button button_1 = new Button(composite_3, SWT.NONE);
		button_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				shlBackOfficeManagement.close();
			}
		});
		button_1.setText("Annuler");
		button_1.setBounds(118, 10, 90, 30);
		m_bindingContext = initDataBindings();

	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap[] observeMaps = BeansObservables.observeMaps(listContentProvider.getKnownElements(), Users.class, new String[]{"name", "prenom", "id"});
		comboViewerUtils.setLabelProvider(new ObservableMapLabelProvider(observeMaps));
		comboViewerUtils.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Users.class).observe(users);
		comboViewerUtils.setInput(selfList);
		//
		ObservableListContentProvider listContentProvider_1 = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider_1.getKnownElements(), Groups.class, "name");
		comboViewergroup.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		comboViewergroup.setContentProvider(listContentProvider_1);
		//
		IObservableList selfList_1 = Properties.selfList(Groups.class).observe(groups);
		comboViewergroup.setInput(selfList_1);
		//
		
		//
		return bindingContext;
	}
	protected DataBindingContext initDataBindingsDate() {
		DataBindingContext bindingContext = new DataBindingContext();
		if (user.getDateEmbauche() != null)
		{
			IObservableValue observeSelectionDateTimeObserveWidget = WidgetProperties.selection().observe(dateTime);
			IObservableValue dateEmbaucheUserObserveValue = BeanProperties.value("dateEmbauche").observe(user);
			bindingContext.bindValue(observeSelectionDateTimeObserveWidget, dateEmbaucheUserObserveValue, null, null);
		}
		//
		return bindingContext;
		}
}
